package com.baichang.taoshunda.controller.mobile;

import com.baichang.framework.controller.BaseMobileController;
import com.baichang.framework.controller.IService;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.controller.ControllerConstants;
import com.baichang.taoshunda.service.InvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by ${wushenglu} on 2018/3/12.
 */
@Controller
@RequestMapping(ControllerConstants.Invoice)
public class InvoiceController extends BaseMobileController {
    @Autowired
    private InvoiceService invoiceService;

    /**
     * 添加发票信息
     *
     * @param request
     * @param response
     */
    @RequestMapping("/addInvoice")
    public void addInvoice(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String invoiceHead = getString(json, "invoiceHead", "");
                String invoiceNumber = getString(json, "invoiceNumber", "");
                Integer invoiceType = getInteger(json, "invoiceType", 0);
                Integer userId = getInteger(json, "userId", 0);
                return invoiceService.addInvoice(invoiceHead, invoiceNumber, invoiceType, userId);
            }
        });
    }

    /**
     * 修改发票信息
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateInvoiceInfo")
    public void updateInvoiceInfo(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String invoiceHead = getString(json, "invoiceHead", "");
                String invoiceNumber = getString(json, "invoiceNumber", "");
                Integer invoiceType = getInteger(json, "invoiceType", 0);
                Integer invoiceId = getInteger(json, "invoiceId", 0);
                return invoiceService.updateInvoiceInfo(invoiceHead, invoiceNumber, invoiceType, 0, invoiceId);
            }
        });
    }

    /**
     * 删除发票信息
     *
     * @param request
     * @param response
     */
    @RequestMapping("/deleteInvoice")
    public void deleteInvoice(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer invoiceId = getInteger(json, "invoiceId", 0);
                return invoiceService.deleteInvoice(invoiceId);
            }
        });
    }

    /**
     * 查询全部的发票信息
     *
     * @param request
     * @param response
     */
    @RequestMapping("/findInvoiceList")
    public void findInvoiceList(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer userId = getInteger(json, "userId", 0);
                return invoiceService.findInvoiceList(userId);
            }
        });
    }

    /**
     * 查询最新的发票信息
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getInvoiceInfo")
    public void getInvoiceInfo(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer userId = getInteger(json, "userId", 0);
                return invoiceService.getInvoiceInfo(userId);
            }
        });
    }

}
